package online.shenjian.hadoop.flow;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author shenjian
 * @since 2023/2/6
 */
public class FlowReduce extends Reducer<Text, FlowBean, Text, FlowBean> {

    private FlowBean valueOut = new FlowBean();

    @Override
    protected void reduce(Text key, Iterable<FlowBean> values, Reducer<Text, FlowBean, Text, FlowBean>.Context context) throws IOException, InterruptedException {
        long totalUp = 0;
        long totalDown = 0;

        for (FlowBean flowBean : values) {
            totalUp += flowBean.getUpFlow();
            totalDown += flowBean.getDownFlow();
        }

        valueOut.setUpFlow(totalUp);
        valueOut.setDownFlow(totalDown);
        valueOut.setTotalFlow();

        context.write(key, valueOut);
    }
}
